<?php

namespace yiike\company\backend\controllers;
use yiike\company\model\TtycMakerSearchController;
use yii\helpers\Url;
use dmstr\bootstrap\Tabs;
use yiike\company\model\TtycMaker;
use common\models\Region;
/**
* This is the class for controller "TtycMakerController".
*/
class TtycMakerController extends \yiike\company\backend\controllers\base\TtycMakerController
{
    public function actionIndex()
    {

        $searchModel  = new TtycMakerSearchController;
        $_GET['TtycMakerSearchController']['company_id'] = \Yii::$app->user->identity->id;
        $dataProvider = $searchModel->search($_GET);

        Tabs::clearLocalStorage();

        Url::remember();
        \Yii::$app->session['__crudReturnUrl'] = null;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionCreate()
    {
        $model = new TtycMaker;

        try {
            if ($model->load($_POST)) {
                $model->company_id = \Yii::$app->user->identity->id;
                $model->uid = 0;
                $model->weid = 2;
                $province = $_POST['TtycMaker']['province'];
                $province = Region::getCityName($province);
                $city = $_POST['TtycMaker']['city'];
                $city = Region::getCityName($city);
                $district = $_POST['TtycMaker']['district'];
                $district = Region::getCityName($district);
                $model->location_p = $province;
                $model->location_c = $city;
                $model->location_a = $district;
                if($model->save()){
                    return $this->redirect(['view', 'id' => $model->id]);
                }

            } elseif (!\Yii::$app->request->isPost) {
                $model->load($_GET);
            }
        } catch (\Exception $e) {
            $msg = (isset($e->errorInfo[2]))?$e->errorInfo[2]:$e->getMessage();
            $model->addError('_exception', $msg);
        }
        return $this->render('create', ['model' => $model]);
    }
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->province = Region::getCityId($model->location_p);
        $model->city = Region::getCity($model->location_c,$model->province);
        $model->district = Region::getCity($model->location_a,$model->city);

        if ($model->load($_POST)){
            $province = $_POST['TtycMaker']['province'];
            $province = Region::getCityName($province);
            $city = $_POST['TtycMaker']['city'];
            $city = Region::getCityName($city);
            $district = $_POST['TtycMaker']['district'];
            $district = Region::getCityName($district);
            $model->location_p = $province;
            $model->location_c = $city;
            $model->location_a = $district;
            if($model->save()){

            return $this->redirect(Url::previous());
             }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    //省市县三级
    public function actions()
    {
        $actions=parent::actions();
        $actions['get-region']=[
            'class'=>\chenkby\region\RegionAction::className(),
            'model'=>\common\models\Region::className()
        ];
        return $actions;
    }
}
